var assert = require('assert');
var crypto = require('crypto');

//3DES加密
function Encrypt(param) {
    var key = new Buffer(param.key);
    var iv = new Buffer(param.iv ? param.iv : 0)
    var plaintext = param.plaintext
    var alg = param.alg
    var autoPad = param.autoPad
    //encrypt
    var cipher = crypto.createCipheriv(alg, key, iv);
    cipher.setAutoPadding(autoPad)  //default true
    var ciph = cipher.update(plaintext, 'utf8', 'hex');
    ciph += cipher.final('hex');
    return ciph;
}

//3DES解密
function Decrypt(param) {
    var key = new Buffer(param.key);
    var iv = new Buffer(param.iv ? param.iv : 0)
    var plaintext = param.plaintext
    var alg = param.alg
    var autoPad = param.autoPad
    //decrypt
    var decipher = crypto.createDecipheriv(alg, key, iv);
    decipher.setAutoPadding(autoPad)
    var txt = decipher.update(plaintext, 'hex', 'utf8');
    txt += decipher.final('utf8');
    return txt;
}
Decrypt({
    key:'12345678123456781234567812345678',
    plaintext: '9L241rcB6wyu5OLp22Cu0nYRvrOebvA/PR4BUzrKb6EJt8t5VZFy9A=='
});
